const WebSocket = require('ws')
const WebSocketServer = WebSocket.Server;

const  wsArr=require("./class/wsArr")
let myWsArr=new wsArr
// 创建 websocket 服务器 监听在 3000 端口
const wss = new WebSocketServer({port: 3001})
const zzArr={}
// 服务器被客户端连接

function init(ws){
	//连接初始化 获取桌号，成功后进行数据交互
	
}
wss.on('connection', (ws) => {
	
	// let t=setInterval(()=>{
	// 	ws.send("1")
	// },1000)
	// setTimeout(()=>{
	// 	ws.close()
	// 	clearInterval(t)
	// },4000)
	ws.onmessage=(({data})=>{
		ws.send(1)
		if(data.includes("close-")){
			let json=JSON.parse(data.substring(6))
			console.log("json:",json);
			myWsArr.exit(json)
		}
		else if(data.includes("code-")){
			try{
				let json=JSON.parse(data.substring(5))
				myWsArr.start(Object.assign(json,{
					ws:ws,
					send:ws.send
				}))
			}catch(err){
				console.log("err:",err);
			}
			// myWs={
			// 	code:Number(data.substring(5)),
			// 	close:ws.close
			// }
		}
	})
})